// 商品详情页

class Goods{
    constructor(){
        this.cont=document.querySelector('.r-items');
    this.getGoods();
    
    }
    // 渲染页面
async getGoods(){
 let data=await axios.get({url:'http://localhost:53000/course1'});
 let html=''
 data.forEach(goods=>{
   
     html+=`
                 <li class="item">
                        <img src="${goods.src}" alt="">
                        <p class="item-name">${goods.name}</p>
                        <p class="item-price">${goods.price}</p>
                        <p class="item-praise">10.4万人好评</p>
                        <div class="item-btn"  onclick="Goods.addCart(${goods.id},1)">
                            加入购物车
                        </div>
                    </li>
     `
     this.cont.innerHTML=html;
 })
}


static addCart(id,num){
    let cartGoods=localStorage.getItem('cart')
    // 判断是否有值
    if(cartGoods){
        //有的话
        cartGoods=JSON.parse(cartGoods);
        for(let attr in cartGoods){
            attr==id &&(num=num+cartGoods[attr])
        }
        cartGoods[id]=num;
        localStorage.setItem('cart',JSON.stringify(cartGoods))
    }else{
        //没有
        cartGoods={[id]:num}
        localStorage.setItem('cart',JSON.stringify(cartGoods))
    }
   
}

 
}
new Goods;